<?php

/**
 * generates sorting links for the listAction() view script
 *
 * @author    Mon Zafra <monzee at gmail>
 * @copyright (c)2009 Mon Zafra
 * @license   http://mz-project.googlecode.com/svn/trunk/LICENSE    MIT License
 * @version   SVN: $Id: SortLink.php 22 2009-05-08 17:08:41Z monzee $
 */
class Crud_View_Helper_SortLink extends Zend_View_Helper_Abstract
{
    /**
     * Returns the link for sorting the table according to $colName
     *
     * @param string $colName
     * @return string
     */
    public function sortLink($colName)
    {
        $params = Zend_Controller_Front::getInstance()->getRequest()->getParams();
        $view = $this->view;

        $url = array('sort' => $colName, 'order' => null);
        if (isset($params['sort']) && $params['sort'] == $colName) {
            if (!isset($params['order']) || strtolower($params['order']) != 'desc') {
                $url['order'] = 'desc';
            }
        }
        $col = $view->inflect($colName)->toTitle();
        $ret = '<a href="' . $view->url($url) . '" title="Sort by '. $col . '">'
             . $col
             . '</a>';

        return $ret;
    }

}
